using System;
using UnityEngine;

[RequireComponent(typeof(AnimatorSprite))]
public class Blood : BloodBase
{
	public static int max = 16;

	public static BloodBase[] listDrops = new BloodBase[0];

	public static Blood[] dropsScene = new Blood[0];

	public int angleHit;

	[Range(0f, 180f)]
	public float angleScatter;

	public float speedOnHit;

	public float addSpeedInPoint;

	public float maxDistSpeed;

	public Vector2 move = default(Vector2);

	public Vector2 gravity = default(Vector2);

	public float floor;

	public Sprite[] sprites = new Sprite[0];

	public SpriteRenderer renderOnFloor;

	private int scaleSpeed;

	private AnimatorSprite animator;

	private int startSpeedAnimation;

	private SpriteRenderer render;

	private string sortingLayerName;

	private float scaleDeltaTime;

	private Transform transformMove;

	private Vector3 lastPositionObject = default(Vector2);

	private void Awake()
	{
		animator = base.gameObject.GetComponent<AnimatorSprite>();
		startSpeedAnimation = animator.speed;
		render = base.gameObject.GetComponent<SpriteRenderer>();
	}

	private void Start()
	{
		base.gameObject.transform.Rotate(new Vector3(0f, 0f, 1f), UnityEngine.Random.Range(0, 360));
		if (renderOnFloor != null)
		{
			renderOnFloor.gameObject.SetActive(value: false);
		}
		if (dropsScene.Length == 0 && base.transform.parent != null)
		{
			dropsScene = base.transform.parent.GetComponentsInChildren<Blood>(includeInactive: true);
		}
	}

	public override void OnGetDrops()
	{
		base.OnGetDrops();
		animator.enabled = true;
		animator.SwitchRandomAnimation();
		animator.GotoAndPlay(1);
		animator.speed = UnityEngine.Random.Range(startSpeedAnimation - 10, startSpeedAnimation);
		render.enabled = true;
		if (renderOnFloor != null)
		{
			renderOnFloor.gameObject.SetActive(value: false);
		}
		float value = UnityEngine.Random.value;
		base.transform.localScale = new Vector3(Mathf.Floor(Mathf.Lerp(60f, 140f, value)) / 100f, Mathf.Floor(Mathf.Lerp(140f, 60f, value)) / 100f);
		base.transform.rotation = Quaternion.Euler(0f, 0f, UnityEngine.Random.Range(-180, 180));
	}

	public override void OnEffectHitBullet(HitBullet hitBullet)
	{
		OnGetDrops();
		Vector3 position = hitBullet.entity.transform.position;
		floor = position.y;
		base.transform.position = hitBullet.raycastHit.point;
		scaleSpeed = 0;
		for (int i = 0; i < dropsScene.Length; i++)
		{
			if (dropsScene[i] != this && dropsScene[i].enabled && Mathf.Abs(Vector2.Distance(dropsScene[i].transform.position, hitBullet.raycastHit.point)) <= maxDistSpeed)
			{
				scaleSpeed = Mathf.Max(dropsScene[i].scaleSpeed, scaleSpeed);
			}
		}
		SetMove(speedOnHit + addSpeedInPoint * (float)scaleSpeed, UnityEngine.Random.Range((float)angleHit - angleScatter, (float)angleHit + angleScatter));
		scaleSpeed++;
	}

	public override void SetMove(float speed, float angle)
	{
		angle *= (float)Math.PI / 180f;
		move.x = Mathf.Cos(angle) * speed;
		move.y = Mathf.Sin(angle) * speed;
		move.x = Mathf.Floor(move.x * 100f) / 100f;
		move.y = Mathf.Floor(move.y * 100f) / 100f;
	}

	public override void SetFloor(float value)
	{
		floor = value;
	}

	public override void AddSpeedBody(Vector2 speed)
	{
		move += speed;
	}

	public void SetObjectMove(Transform transform)
	{
		transformMove = transform;
		lastPositionObject = transform.position;
	}

	private void Update()
	{
		scaleDeltaTime = Time.deltaTime * Time.timeScale;
		if (transformMove != null && lastPositionObject.magnitude != transformMove.position.magnitude)
		{
			ref Vector2 reference = ref move;
			float x = reference.x;
			Vector3 position = transformMove.position;
			reference.x = x + (position.x - lastPositionObject.x) / scaleDeltaTime;
			ref Vector2 reference2 = ref move;
			float y = reference2.y;
			Vector3 position2 = transformMove.position;
			reference2.y = y + (position2.y - lastPositionObject.y) / scaleDeltaTime;
			transformMove = null;
		}
		move += gravity * scaleDeltaTime;
		base.transform.position += new Vector3(move.x * scaleDeltaTime, move.y * scaleDeltaTime, 0f);
		Vector3 position3 = base.gameObject.transform.position;
		if (!(position3.y < floor))
		{
			return;
		}
		animator.Stop();
		animator.enabled = false;
		render.enabled = false;
		base.enabled = false;
		base.gameObject.transform.rotation = default(Quaternion);
		if (!(renderOnFloor != null) || sprites.Length == 0)
		{
			return;
		}
		renderOnFloor.sprite = sprites[animator.currentFrame - 1];
		renderOnFloor.flipX = ((double)UnityEngine.Random.value > 0.5);
		renderOnFloor.flipY = ((double)UnityEngine.Random.value > 0.5);
		render.flipX = renderOnFloor.flipX;
		render.flipY = renderOnFloor.flipY;
		int num = 0;
		for (int i = 0; i < dropsScene.Length; i++)
		{
			if (num >= 2)
			{
				break;
			}
			if (dropsScene[i] != this && dropsScene[i].renderOnFloor != null && dropsScene[i].renderOnFloor.gameObject.activeInHierarchy && Mathf.Abs(Vector2.Distance(dropsScene[i].transform.position, base.transform.position)) <= maxDistSpeed)
			{
				num++;
			}
		}
		renderOnFloor.gameObject.SetActive(num < 2);
		base.gameObject.SetActive(num < 2);
	}

	private void OnDestroy()
	{
		dropsScene = new Blood[0];
		listDrops = new BloodBase[0];
	}

	public override BloodBase[] GetListObjects()
	{
		if (max > 0 && listDrops.Length == 0)
		{
			listDrops = new BloodBase[max];
		}
		else if (max <= 0 && listDrops.Length != 0)
		{
			listDrops = new BloodBase[0];
		}
		return listDrops;
	}
}
